<?php
// $Id$

/**
 * Controller_Ucenterapi controller
 */
class Controller_Ucenterapi extends Controller_Abstract
{
    function actionTest()
    {
        return UC_API_RETURN_SUCCEED;
    }

    /**
     * 删除用户
     */
	function actionDeleteUser()
    {
        QLog::log(__METHOD__, QLog::DEBUG);
        $ids = Q::normalize($_GET['ids']);
        Member::meta()->destroyWhere(array('id' => $ids));
        return UC_API_RETURN_SUCCEED;
    }

    /**
     * 同步登录
     */
    function actionSynLogin()
    {
        QLog::log(__METHOD__, QLog::DEBUG);
        // 同步登录时，应该优先从本地数据库验证用户信息
        $member = Member::meta()->validateLogin($_GET['username'],
                                                $_GET['password'],
                                                UCenterClient::NOT_UID,
                                                UCenterClient::LOCAL_DB);
        QLog::log(__METHOD__ . ': after query', QLog::DEBUG);
        if ($member->id())
        {
            QLog::log(__METHOD__ . ': ' . $member->toJSON(0), QLog::DEBUG);
            header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
            $this->_app->changeCurrentUser($member->aclData(), $member->aclRoles());
            return UC_API_RETURN_SUCCEED;
        }
        else
        {
            return UC_API_RETURN_FAILED;
        }
    }

    /**
     * 同步注销
     */
    function actionSynLogout()
    {
        QLog::log(__METHOD__, QLog::DEBUG);
        $this->_app->cleanCurrentUser();
        return UC_API_RETURN_SUCCEED;
    }

    /**
     * 更新密码
     */
    function actionUpdatePW()
    {
        QLog::log(__METHOD__, QLog::DEBUG);
        Member::meta()->updatePassword($_GET['username'], $_GET['password'], UCenterClient::LOCAL_DB);
        return UC_API_RETURN_SUCCEED;
    }

    /**
     * 更新应用程序列表
     */
    function actionUpdateApps()
    {
        QLog::log(__METHOD__, QLog::DEBUG);
        $filename = UC_CLIENT_DIR . '/data/cache/apps.php';
        QLog::log('update apps cache file: ' . $filename, QLog::DEBUG);
        $content = "<?php\n\$_CACHE['apps'] = " . var_export($_POST, true) . ";\n\n";
        if (file_put_contents($filename, $content, LOCK_EX))
        {
            return UC_API_RETURN_SUCCEED;
        }
        else
        {
            return UC_API_RETURN_FAILED;
        }
    }

    /**
     * 更新客户端设置
     */
    function actionUpdateClient()
    {
        QLog::log(__METHOD__, QLog::DEBUG);
        $filename = UC_CLIENT_DIR . '/data/cache/setting.php';
        QLog::log('update apps cache file: ' . $filename, QLog::DEBUG);
        $content = "<?php\n\$_CACHE['setting'] = " . var_export($_POST, true) . ";\n\n";
        if (file_put_contents($filename, $content, LOCK_EX))
        {
            return UC_API_RETURN_SUCCEED;
        }
        else
        {
            return UC_API_RETURN_FAILED;
        }
    }

}


